﻿using System.Web;
using System.Web.Security;
using System.Collections.Generic;
using WebMatrix.WebData;
using System.Configuration;
using UnliQ.Service.Contracts;

namespace UnliQ.Web
{
    public static class UsersConfig
    {
        #region [Private Member(s)]

        private static IUserService _userService = null;
        private static List<UserDTO> _userList = null;

        private static List<string> _roles = new List<string>
        {
            "Admin", "Subscriber", "Trigger"
        };
       
        private static void InitRoles()
        {
            _roles.ForEach(r =>
            {
                if (!Roles.RoleExists(r))
                    Roles.CreateRole(r);
            });
        }

        private static void InitSuperUser()
        {
            var username = ConfigurationManager.AppSettings["superuser"];

            if (!WebSecurity.UserExists(username))
            {
                WebSecurity.CreateUserAndAccount(username, ConfigurationManager.AppSettings["superuserPassword"]);
                Roles.AddUserToRoles(username, _roles.ToArray());

                var id = WebSecurity.GetUserId(username);
                _userList.Add(new UserDTO
                {
                    UserId = id,
                    Username = username
                });
            }
        }

        private static void InitSubscriber()
        {
            var username = "testSubscriber";
            var password = "12345";

            if (!WebSecurity.UserExists(username))
            {
                WebSecurity.CreateUserAndAccount(username, password);
                Roles.AddUserToRole(username, "Subscriber");

                var id = WebSecurity.GetUserId(username);
                _userList.Add(new UserDTO
                {
                    UserId = id,
                    Username = username
                });
            }    
        }

        private static void InitTriggerUser()
        {
            var username = "testTrigger";
            var password = "12345";

            if (!WebSecurity.UserExists(username))
            {
                WebSecurity.CreateUserAndAccount(username, password);
                Roles.AddUserToRole(username, "Trigger");

                var id = WebSecurity.GetUserId(username);
                _userList.Add(new UserDTO
                {
                    UserId = id,
                    Username = username
                });
            }    
        }

        private static void InitOtherUsers()
        {
            InitSubscriber();
            InitTriggerUser();

            _userService.AddInitValues(_userList);
        }

        #endregion      
        
        public static void InitializeUsers(IUserService service)
        {
            _userService = service;
            _userList = new List<UserDTO>();

            InitRoles();
            InitSuperUser();
            InitOtherUsers();
        }     

    }
}